﻿using System;
using HCI.Questionaire.Exceptions;
using System.ComponentModel;

namespace HCI.Questionaire.Entities
{
    public class RatingQuestion : Question, INotifyPropertyChanged
    {
        public double Rate
        {
            get 
            {
                return this.AnswerCollection[0].AnswerResult != null ? (double)this.AnswerCollection[0].AnswerResult : 0.0;
            }
            set {
                this.AnswerCollection[0].AnswerResult = value;
                OnPropertyChanged("Rate");
            }
        }

        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }

        }

        public override void ValidateAnswer()
        {
            if (this.AnswerCollection != null && this.AnswerCollection.Count != 1)
            {
                throw new AnswerInvalidException("Answer in the collection must have one answer");
            }

            // TODO: Should not fix result value?
            double answerResult = Convert.ToDouble(this.AnswerCollection[0].AnswerResult);
            if (!(answerResult >= 1 && answerResult <= 5))
            {
                throw new AnswerInvalidException("Rating must be 0-5.");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
